package com.ecoolex.discount.sales.admin.graphql;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountProductInventory;
import com.ecoolex.discount.sales.entity.DiscountProductWithBLOBs;
import com.ecoolex.discount.sales.enums.DiscountProductInventoryStatus;
import com.ecoolex.discount.sales.pojo.AddDiscountProductInventoryDTO;
import com.ecoolex.discount.sales.pojo.EditDiscountProductInventoryDTO;
import com.ecoolex.discount.sales.pojo.QueryDiscountProductInventoryDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import java.util.List;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "折扣产品库存管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
        key = DiscountProductInventoryGraphql.AUTH,
        name = "折扣产品库存管理",
        remark = "折扣产品库存管理")
public class DiscountProductInventoryGraphql {

    public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-DISCOUNT-PRODUCT-INVENTORY";

    public static final String AUTH_GET = AUTH + "-GET";
    public static final String AUTH_QUERY = AUTH + "-QUERY";
    public static final String AUTH_ADD = AUTH + "-ADD";
    public static final String AUTH_EDIT = AUTH + "-EDIT";
    public static final String AUTH_INVALID = AUTH + "-INVALID";

    @Reference
    private DiscountProductService discountProductService;

    @TypeField(type = DiscountProductInventory.class, field = "canInvalid", description = "是否能作废")
    public boolean canInvalid(@Source DiscountProductInventory source) {
        return DiscountProductInventoryStatus.canInvalid(source);
    }

    @TypeField(type = DiscountProductInventory.class, field = "canEdit", description = "是否能编辑")
    public boolean canEdit(@Source DiscountProductInventory source) {
        return DiscountProductInventoryStatus.canEdit(source);
    }

    @TypeField(type = DiscountProductInventory.class, field = "canDel", description = "是否能删除")
    public boolean canDel(@Source DiscountProductInventory source) {
        return DiscountProductInventoryStatus.canDel(source);
    }

    @TypeField(type = DiscountProductInventory.class, field = "product", description = "产品")
    public DiscountProductWithBLOBs product(@Source DiscountProductInventory source) {
        return discountProductService.getProductById(source.getProductId());
    }

    @QueryField(field = "discountProductInventory", description = "获取折扣产品库存详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取折扣产品库存详情")
    public DiscountProductInventory getById(@NonNull Integer inventoryId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return discountProductService.getInventoryById(inventoryId);
    }

    @QueryField(field = "discountProductInventoryList", description = "查询折扣产品库存列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询折扣产品库存列表")
    public Pagination<DiscountProductInventory> query(@NonNull QueryDiscountProductInventoryDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return discountProductService.queryInventory(query, sorts, page);
    }

    @MutationField(field = "addDiscountProductInventory", description = "添加折扣产品库存")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加折扣产品库存")
    public boolean add(@NonNull Integer productId, @NonNull AddDiscountProductInventoryDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        discountProductService.addInventory(productId, add, user.userName());
        return true;
    }

    @MutationField(field = "batchAddDiscountProductInventory", description = "批量添加折扣产品库存")
    public boolean add(@NonNull Integer productId, @NonNull List<AddDiscountProductInventoryDTO> addList, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        discountProductService.batchAddInventory(productId, addList, user.userName());
        return true;
    }

    @MutationField(field = "editDiscountProductInventory", description = "编辑折扣产品库存")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑折扣产品库存")
    public boolean update(@NonNull Integer inventoryId, @NonNull EditDiscountProductInventoryDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        discountProductService.updateInventory(inventoryId, edit, user.userName());
        return true;
    }

    @MutationField(field = "invalidDiscountProductInventory", description = "作废折扣产品库存")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_INVALID, name = "作废折扣产品库存")
    public boolean add(@NonNull Integer inventoryId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_INVALID);
        discountProductService.invalidInventory(inventoryId, user.userName());
        return true;
    }

}
